//
//  LMHomeViewController.swift
//  WeiBo
//
//  Created by Li MAc on 2018/11/13.
//  Copyright © 2018 Li MAc. All rights reserved.
//

import UIKit

private let cellID = "cellid"
private let zfcellID = "zfcellid"

class LMHomeViewController: LMBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
    }
    
    //添加设置用户名title
    private func setupTitele(){
        let navtitle = LMNetworkTools.shared.userSG.screen_name
       
        let titltBut = LMTitleButton(title: navtitle!)
        titltBut.setTitleColor(UIColor.gray, for: .normal)
//        titltBut.setTitleColor(UIColor.orange, for: .selected)
        navItem.titleView = titltBut
    }
    
    //懒加载列表数据b模型
    private lazy var statusModel = LMStatusViewMode()
        
     @objc override func loadDataa(){
        
//        print("准备刷新，最后一条\(self.statusModel.stat.last?.text)")
        
        
        //当用户登陆成功后将登陆按钮设置为空
        navItem.rightBarButtonItem = nil
        statusModel.lodStatus(stretchingUp: self.stretchingUp) { (isSuccess,hasStatusUp) in
            //结束刷新
                        self.refresh?.endRefreshing()
            
                        self.stretchingUp = false
            
                        //刷新表格
            if hasStatusUp {
                 self.tableView?.reloadData()
            }
            
        }
        
//        let urlstring = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let parma = ["access_token":"2.002pZ7AH0Ie1bXca05ebb730veEflC"]
//
//        LMNetworkTools.shared.get(urlstring, parameters: parma, progress: nil, success: { (_, json) in
//            print(json)
//        }) { (_, error) in
//            print("网络请求失败\(error)")
//        }
//        print("开始加载数据\(LMNetworkTools.shared)")

//        LMNetworkTools.shared.request(URLString: urlstring, parameters: parma as [String : AnyObject]) { (josn, isSuccess) in
//            print(josn)
//        }
//
////        延时一秒
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
//
//            for i in 0...30 {
//                if self.stretchingUp {
//                    self.statusList.append("上拉\(i)")
//                }else{
//                self.statusList.insert(i.description, at: 0)
//                }
//
//            }
//            //结束刷新
//            self.refresh?.endRefreshing()
//
//            self.stretchingUp = false
//
//            //刷新表格
//            self.tableView?.reloadData()
//        }
        
    }
    
    @objc private func navItemFunc(){
        let demo = LMDemoViewController()
        
        //隐藏下面的tabbar
        //        demo.hidesBottomBarWhenPushed = true
        //利用重写push方法隐藏底部的tabBar  （Navigtion方法中重写）
        navigationController?.pushViewController(demo, animated: true)
        
    }
}


extension LMHomeViewController {
    
   //重写父类方法
   override func setupTableView() {
        super.setupTableView()
   
        //    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(navItem))
        //    navigationItem.leftBarButtonItem?.tintColor = UIColor.orange

        //    let leftBut:UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        //
        //    leftBut.addTarget(self, action: #selector(navItem), for: .touchUpInside)
        //
        //
        //    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBut)


        //    -------> UIBarButtonItem+Extensions <-------
    navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fotSize: 16 , target: self, action: #selector(navItemFunc) , isBack:false)
    
    //注册原型 cell
//    tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    tableView?.register(UINib(nibName: "Record", bundle: nil), forCellReuseIdentifier: cellID)
    tableView?.register(UINib(nibName: "BZrecord", bundle: nil), forCellReuseIdentifier: zfcellID)
    //设置行行高
    tableView?.rowHeight = UITableView.automaticDimension
    tableView?.estimatedRowHeight = 300
    //取消tableView两个数据间的线分割线
    tableView?.separatorStyle = .none
    
    //当用户登陆成功时，加载h用户名
    setupTitele()
    }
}
    

extension LMHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusModel.stat.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let vv = statusModel.stat[indexPath.row]
        
        let c = (vv.status.retweeted_status == nil) ? cellID : zfcellID
        
        let cell = tableView.dequeueReusableCell(withIdentifier: c, for: indexPath) as! LMRecordTableViewCell
        
        
        cell.recordviewModel = vv
      
        
        return cell
        
    }
    
    
}


